/**
 * Copyright &copy; 2012-2014 <a href="https://github.cn.apestar">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.utils.StringUtils;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.sys.entity.UserAndInstituion;
import cn.apestar.modules.sys.service.UserAndInstituionService;

/**
 * 人员和机构关系Controller
 * @author LVH
 * @version 2016-03-25
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/userAndInstituion")
public class UserAndInstituionController extends BaseController {

	@Autowired
	private UserAndInstituionService userAndInstituionService;
	
	@ModelAttribute
	public UserAndInstituion get(@RequestParam(required=false) String id) {
		UserAndInstituion entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = userAndInstituionService.get(id);
		}
		if (entity == null){
			entity = new UserAndInstituion();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:userAndInstituion:view")
	@RequestMapping(value = {"list", ""})
	public String list(UserAndInstituion userAndInstituion, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<UserAndInstituion> page = userAndInstituionService.findPage(new Page<UserAndInstituion>(request, response), userAndInstituion); 
		model.addAttribute("page", page);
		return "modules/sys/userAndInstituionList";
	}

	@RequiresPermissions("sys:userAndInstituion:view")
	@RequestMapping(value = "form")
	public String form(UserAndInstituion userAndInstituion, Model model) {
		model.addAttribute("userAndInstituion", userAndInstituion);
		return "modules/sys/userAndInstituionForm";
	}

	@RequiresPermissions("sys:userAndInstituion:edit")
	@RequestMapping(value = "save")
	public String save(UserAndInstituion userAndInstituion, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, userAndInstituion)){
			return form(userAndInstituion, model);
		}
		userAndInstituionService.save(userAndInstituion);
		addMessage(redirectAttributes, "保存人员和机构关系成功");
		return "redirect:"+Global.getAdminPath()+"/sys/userAndInstituion/?repage";
	}
	
	@RequiresPermissions("sys:userAndInstituion:edit")
	@RequestMapping(value = "delete")
	public String delete(UserAndInstituion userAndInstituion, RedirectAttributes redirectAttributes) {
		userAndInstituionService.delete(userAndInstituion);
		addMessage(redirectAttributes, "删除人员和机构关系成功");
		return "redirect:"+Global.getAdminPath()+"/sys/userAndInstituion/?repage";
	}

}